from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.edge.webdriver import WebDriver

from selenium_study.tools.static import Constvalue

wd=WebDriver()
wd.implicitly_wait(10)

wd.get(Constvalue.url.value)
#使用CSS选择器件
username=wd.find_element(By.CSS_SELECTOR,'#username')
password=wd.find_element(By.CSS_SELECTOR,'#password')
username.send_keys(Constvalue.user.value)
password.send_keys(Constvalue.password.value)
login=wd.find_element(By.XPATH,'/html/body/div[1]/div[2]/div[1]/div[3]/div/button').click()

#保存句柄
mainhadle=wd.current_window_handle
#跳转页面
otherpage=wd.find_element(By.CSS_SELECTOR,'#root > footer > div > a').click()
#转换页面驱动
for handle in wd.window_handles:
    # 先切换到该窗口
    wd.switch_to.window(handle)
    # 得到该窗口的标题栏字符串，判断是不是我们要操作的那个窗口
    if '白月黑羽' in wd.title:
        # 如果是，那么这时候WebDriver对象就是对应的该该窗口，正好，跳出循环，
        break
#找导航元素
sleep(1)
wd.maximize_window()
res=''
navigation = wd.find_elements(By.CSS_SELECTOR, 'ul a')
for element in navigation:
    res+=''.join(element.text)
#断言
print("实际结果"+res)
expected_result = 'Python基础Python进阶Qt图形界面Django自动化测试性能测试JS语言JS前端'
print('预期结果为：', expected_result)
try:
    assert expected_result == res
    print('UI_0106 PASS,实际结果与预期结果一致!')
except Exception as e:
    print('UI_0106 FAIL,实际结果与预期结果不一致!', format(e))
#返回主界面
wd.switch_to.window(mainhadle)
#退出,先点击管理页面，再点击退出
sleep(3)
wd.find_element(By.CSS_SELECTOR,'#root > header > nav > div > ul > li.dropdown.user.user-menu > a').click()
sleep(3)
wd.find_element(By.CSS_SELECTOR,'#root > header > nav > div > ul > '
                                'li.dropdown.user.user-menu.open > ul > '
                                'li.user-footer > div.pull-right > a').click()
sleep(1)
input("111")  #wd.quit()